﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using skhukb.DB;
using skhukb.BL;
using skhukb.Models;

namespace skhukb.Controllers
{
    [Authorize(Roles = MyRoleProvider.ADMIN)]
    public class BbsController : Controller
    {
        const string 삭제실패 = "게시글이 존재하므로 삭제할 수 없습니다";
        const string 새제목 = "새 게시판";
        const string 미분류 = "미분류";

        [HttpGet]
        public ActionResult Edit(int id)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.FirstOrDefault(p => p.ID == id);
                if (bbs == null) return HttpNotFound();

                var 모델 = new BbsEditModel() {
                    게시글분류목록 = BL.게시판.게시글분류목록(id),
                    게시판 = new 게시판Model(bbs)
                };
                return View(모델);
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult UpdateTitle(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.FirstOrDefault(p => p.ID == id);
                if (bbs == null) return HttpNotFound();

                bbs.제목 = title;
                db.SubmitChanges();
                return Json(new JsonModel { success = true });
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult DeleteCategory(int id)
        {
            using (var db = new DBDataContext()) {
                var 항목 = db.게시글분류.FirstOrDefault(p => p.ID == id);
                if (항목 == null) return HttpNotFound();
                if (항목.게시글.Count > 0) return Json(new JsonModel { success = false, message = 삭제실패 });

                db.게시글분류.DeleteOnSubmit(항목);
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BbsEdit(항목.게시판ID) });
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult UpdateCategory(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var 항목 = db.게시글분류.FirstOrDefault(p => p.ID == id);
                if (항목 == null) return HttpNotFound();

                항목.제목 = title;
                db.SubmitChanges();
                return Json(new JsonModel { success = true });
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult AddCategory(int id, string title)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.FirstOrDefault(p => p.ID == id);
                if (bbs == null) return HttpNotFound();

                var 항목 = new 게시글분류 { 게시판ID = id, 제목 = title };
                db.게시글분류.InsertOnSubmit(항목);
                db.SubmitChanges();
                return Json(new JsonModel { success = true, url = Url.BbsEdit(bbs.ID) });
            }
        }


        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult New()
        {
            using (var db = new DBDataContext()) {
                var bbs = new skhukb.DB.게시판 { 제목 = 새제목 };
                db.게시판.InsertOnSubmit(bbs);
                db.SubmitChanges();

                db.게시글분류.InsertOnSubmit(new 게시글분류 { 게시판ID = bbs.ID, 제목 = 미분류 });
                db.SubmitChanges();

                return Json(new JsonModel { success = true, url = Url.BbsEdit(bbs.ID) });
            }
        }

        [HttpPost]
        [Util.AjaxOnly]
        public ActionResult Delete(int id)
        {
            using (var db = new DBDataContext()) {
                var bbs = db.게시판.FirstOrDefault(p => p.ID == id);
                if (bbs == null) return HttpNotFound();
                if (bbs.게시글.Count > 0) return Json(new JsonModel { success = false, message = 삭제실패 });

                db.게시글분류.DeleteAllOnSubmit(bbs.게시글분류);
                db.게시판.DeleteOnSubmit(bbs);
                db.SubmitChanges();

                return Json(new JsonModel { success = true, url = "/" });
            }
        }


    }
}
